##########################################################################################
###  Replication code for:
###  Supporting Information for "Do Primaries Improve Electoral Performance? Clientelism
###  and Intra-Party Conflict in Ghana," American Journal of Political Science
###
###  IchinoNathan_AJPS_primariesSI_matching.R
###
###  Nahomi Ichino and Noah Nathan
###  Department of Government, Harvard University
###  June 2012 
###
###  Note: It is *not* necessary to run this code before doing the replication analysis in
###  IchinoNathan_AJPS_primariesSI_analysis.R.  The matching output produced by this code are 
###  saved as .Rdata files.
###
###  Calls the data files:
###  primariesdata_NDC.csv
###  primariesdata_NPP.csv
###   
###  Produces:
###  matchingoutput_t3SI.Rdata
###  matchingoutput_t4SI.Rdata
###  matchingoutput_t56SI.Rdata
###  matchingoutput_t56SIc.Rdata
###
###  We use:
###  Matching (Version 4.8-0, Build Date: 2012/01/19)
###  See http://sekhon.berkeley.edu/matching for additional documentation.
###   Jasjeet S. Sekhon. 2011. ``Multivariate and Propensity Score Matching
###   Software with Automated Balance Optimization: The Matching package for R.''
###   Journal of Statistical Software, 42(7): 1-52.
##########################################################################################



##########################################################################################
###  For SI Table 3: NDC Subsets
##########################################################################################

library(Matching)
data2 <- read.csv("primariesdata_CNDC.csv") 

## Competitive NDC
data2 <- read.csv("primariesdata_NDC.csv") 
data2 <-subset(data2, data2$pres_t_1<.6)
data2 <-subset(data2, data2$pres_t_1_other<.6)

## Stronghold NDC
data2a <- read.csv("primariesdata_NDC.csv") 
data2a <-subset(data2a, data2a$pres_t_1>.6)

## exact match on the last three variables in x (the matching variables) defined below
ev.ndc<-c(FALSE,FALSE,TRUE,TRUE,TRUE)


##########################################
## NDC COMPETITIVE ONLY (ATE)

set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.2.ATE<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##########################################
## NDC COMPETITIVE ONLY (ATT)

set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.2<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##########################################
## NDC STRONGHOLD ONLY (ATE)

set.seed(20110111)
x<-cbind(data2a$pres_t_1, data2a$ethfrac, data2a$year2008, data2a$holds1, data2a$incumbent)
genout.2a.ATE<-GenMatch(Tr=data2a$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##########################################
## NDC STRONGHOLD ONLY (ATT)

set.seed(20110111)
x<-cbind(data2a$pres_t_1, data2a$ethfrac, data2a$year2008, data2a$holds1, data2a$incumbent)
genout.2a<-GenMatch(Tr=data2a$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)

#####################################################################
### save workspace
save(data2, data2a, genout.2, genout.2a, genout.2.ATE, genout.2a.ATE, file="matchingoutput_t3SIa.Rdata")




##########################################################################################
###  For SI Table 4: Using Previous Parliamentary Vote Share
##########################################################################################

library(Matching)
rm(list=ls())
ev<-c(FALSE,FALSE,TRUE,TRUE,TRUE)
ev.ndc<-c(FALSE,FALSE,TRUE,TRUE,TRUE)

data2 <- read.csv("primariesdata_NDC.csv")
data2<-subset(data2, data2$vote_share_t_1>0)

data1 <- read.csv("primariesdata_NPP.csv") 
data1<-subset(data1, data1$vote_share_t_1>0)

data.NNPP <- read.csv("primariesdata_NPP.csv") 
data.NNPP <- subset(data.NNPP, data.NNPP$pres_t_1>.6)
data.NNPP <-subset(data.NNPP, data.NNPP$vote_share_t_1!=0)

data.CNPP <- read.csv("primariesdata_NPP.csv") 
data.CNPP <- subset(data.CNPP, data.CNPP$pres_t_1<.6)
data.CNPP <- subset(data.CNPP, data.CNPP$pres_t_1_other<.6)
data.CNPP <-subset(data.CNPP, data.CNPP$vote_share_t_1!=0)




#####################
## 2 NDC (ATE)
#####################

set.seed(20110111)
x<-cbind(data2$vote_share_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.NDC.ATE<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)


#####################
## 2 NDC (ATT)
#####################

set.seed(20110111)
x<-cbind(data2$vote_share_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.NDC.ATT<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)


#####################
## 1 NPP only (ATE)
#####################

set.seed(20110111)
x<-cbind(data1$vote_share_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
genout.NPP.ATE<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)


#####################
## 1 NPP only (ATT)
#####################

set.seed(20110111)
x<-cbind(data1$vote_share_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
genout.NPP.ATT<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)


##########################################
## NPP non-competitive (ATE)
##########################################

set.seed(20110111)
x<-cbind(data.NNPP$vote_share_t_1, data.NNPP$ethfrac, data.NNPP$year2008, data.NNPP$holds1, data.NNPP$incumbent)
genout.NNPP.ATE<-GenMatch(Tr=data.NNPP$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)


##########################################
## NPP non-competitive (ATT)
##########################################

set.seed(20110111)
x<-cbind(data.NNPP$vote_share_t_1, data.NNPP$ethfrac, data.NNPP$year2008, data.NNPP$holds1, data.NNPP$incumbent)
genout.NNPP.ATT<-GenMatch(Tr=data.NNPP$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)


##########################################
## NPP competitive (ATE)
##########################################

set.seed(20110111)
x<-cbind(data.CNPP$vote_share_t_1, data.CNPP$ethfrac, data.CNPP$year2008, data.CNPP$holds1, data.CNPP$incumbent)
genout.CNPP.ATE<-GenMatch(Tr=data.CNPP$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##########################################
## NPP competitive (ATT)
##########################################

set.seed(20110111)
x<-cbind(data.CNPP$vote_share_t_1, data.CNPP$ethfrac, data.CNPP$year2008, data.CNPP$holds1, data.CNPP$incumbent)
genout.CNPP.ATT<-GenMatch(Tr=data.CNPP$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)


#####################################################################
### save workspace

save(data1, data2, data.CNPP, data.NNPP, genout.NDC.ATE, genout.NDC.ATT, genout.NPP.ATE, genout.NPP.ATT, genout.NNPP.ATE, genout.NNPP.ATT,genout.CNPP.ATE, genout.CNPP.ATT, file="matchingoutput_t4SI.Rdata")







##########################################################################################
###  For SI Tables 5 and 6 (Panels A and B): Using Additional Matching Variables
##########################################################################################

library(Matching)
rm(list=ls())

ev.ndc<-c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)
ev<-c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE)

################################################################
## NDC only (ATE)

data2 <- read.csv("primariesdata_NDC.csv") 
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$english_percent, data2$urban, data2$year2008, data2$ethfrac, data2$public_semipublic_p, data2$dispute_before, data2$holds1, data2$log_MPdist, data2$prim_other_total, data2$incumbent)
genout.2.ATE<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
## NDC only (ATT)

data2 <- read.csv("primariesdata_NDC.csv") 
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$english_percent, data2$urban, data2$year2008, data2$ethfrac, data2$public_semipublic_p, data2$dispute_before, data2$holds1, data2$log_MPdist, data2$prim_other_total, data2$incumbent)

genout.2<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
## NPP only (ATE)

data1 <- read.csv("primariesdata_NPP.csv") 
set.seed(20110111)
x<-cbind(data1$pres_t_1, data1$english_percent, data1$urban, data1$year2008, data1$ethfrac, data1$public_semipublic_p, data1$dispute_before, data1$holds1, data1$log_MPdist, data1$prim_other_total, data1$incumbent, data1$min_inc_alt)
genout.1.ATE<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
## NPP only (ATT)

data1 <- read.csv("primariesdata_NPP.csv") 
set.seed(20110111)
x<-cbind(data1$pres_t_1, data1$english_percent, data1$urban, data1$year2008, data1$ethfrac, data1$public_semipublic_p, data1$dispute_before, data1$holds1, data1$log_MPdist, data1$prim_other_total, data1$incumbent, data1$min_inc_alt)
genout.1<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
## Stronghold NPP (ATE)

data.nnpp <- read.csv("primariesdata_NPP.csv") 
data.nnpp <- subset(data.nnpp, data.nnpp$pres_t_1>.6)
set.seed(20110111)
x<-cbind(data.nnpp$pres_t_1, data.nnpp$english_percent, data.nnpp$urban, data.nnpp$year2008, data.nnpp$ethfrac, data.nnpp$public_semipublic_p, data.nnpp$dispute_before, data.nnpp$holds1, data.nnpp$log_MPdist, data.nnpp$prim_other_total, data.nnpp$incumbent, data.nnpp$min_inc_alt)
genout.nnpp.ATE<-GenMatch(Tr=data.nnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
##  Stronghold NPP (ATT)

data.nnpp <- read.csv("primariesdata_NPP.csv") 
data.nnpp <- subset(data.nnpp, data.nnpp$pres_t_1>.6)
 
set.seed(20110111)
x<-cbind(data.nnpp$pres_t_1, data.nnpp$english_percent, data.nnpp$urban, data.nnpp$year2008, data.nnpp$ethfrac, data.nnpp$public_semipublic_p, data.nnpp$dispute_before, data.nnpp$holds1, data.nnpp$log_MPdist, data.nnpp$prim_other_total, data.nnpp$incumbent, data.nnpp$min_inc_alt)
genout.nnpp<-GenMatch(Tr=data.nnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)


###############################################################
## Competitive NPP only (ATE)

data.cnpp <- read.csv("primariesdata_NPP.csv") 
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1<.6)
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data.cnpp$pres_t_1, data.cnpp$english_percent, data.cnpp$urban, data.cnpp$year2008, data.cnpp$ethfrac, data.cnpp$public_semipublic_p, data.cnpp$dispute_before, data.cnpp$holds1, data.cnpp$log_MPdist, data.cnpp$prim_other_total, data.cnpp$incumbent, data.cnpp$min_inc_alt)
genout.cnpp.ATE<-GenMatch(Tr=data.cnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

###############################################################
### Competitive NPP only

data.cnpp <- read.csv("primariesdata_NPP.csv") 
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1<.6)
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data.cnpp$pres_t_1, data.cnpp$english_percent, data.cnpp$urban, data.cnpp$year2008, data.cnpp$ethfrac, data.cnpp$public_semipublic_p, data.cnpp$dispute_before, data.cnpp$holds1, data.cnpp$log_MPdist, data.cnpp$prim_other_total, data.cnpp$incumbent, data.cnpp$min_inc_alt)
genout.cnpp<-GenMatch(Tr=data.cnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)

###############################################################
#### save workspace
save(data1, data2, data.cnpp, data.nnpp, genout.1, genout.2, genout.cnpp, genout.nnpp, genout.1.ATE, genout.2.ATE, genout.cnpp.ATE, genout.nnpp.ATE, file="matchingoutput_t56SI.Rdata")




##########################################################################################
###  For SI Tables 5 and 6 (Panel C): Using Additional Matching Variables
##########################################################################################



library(Matching)
rm(list=ls())
ev.ndc<-c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)

## Competitive NDC
data2 <- read.csv("primariesdata_NDC.csv") 
data2 <-subset(data2, data2$pres_t_1<.6)
data2 <-subset(data2, data2$pres_t_1_other<.6)

## Stronghold NDC
data2a <- read.csv("primariesdata_NDC.csv") 
data2a <-subset(data2a, data2a$pres_t_1>.6)


################################################################
### Competitive NDC (ATE)

data2 <- read.csv("primariesdata_NDC.csv") 
data2 <-subset(data2, data2$pres_t_1<.6)
data2 <-subset(data2, data2$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$english_percent, data2$urban, data2$year2008, data2$ethfrac, data2$public_semipublic_p, data2$dispute_before, data2$holds1, data2$log_MPdist, data2$prim_other_total, data2$incumbent)
genout.2.ATE<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
### Competitive NDC (ATT)

data2 <- read.csv("primariesdata_NDC.csv") 
data2 <-subset(data2, data2$pres_t_1<.6)
data2 <-subset(data2, data2$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$english_percent, data2$urban, data2$year2008, data2$ethfrac, data2$public_semipublic_p, data2$dispute_before, data2$holds1, data2$log_MPdist, data2$prim_other_total, data2$incumbent)
genout.2<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
### Stronghold NDC (ATE)

data2a <- read.csv("primariesdata_NDC.csv") 
data2a <-subset(data2a, data2a$pres_t_1>.6) 
set.seed(20110111)
x<-cbind(data2a$pres_t_1, data2a$english_percent, data2a$urban, data2a$year2008, data2a$ethfrac, data2a$public_semipublic_p, data2a$dispute_before, data2a$holds1, data2a$log_MPdist, data2a$prim_other_total, data2a$incumbent)
genout.2a.ATE<-GenMatch(Tr=data2a$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=500, max.generations=30, wait.generations=10)

################################################################
### Stronghold NDC (ATT)

data2a <- read.csv("primariesdata_NDC.csv") 
data2a <-subset(data2a, data2a$pres_t_1>.6)
set.seed(20110111)
x<-cbind(data2a$pres_t_1, data2a$english_percent, data2a$urban, data2a$year2008, data2a$ethfrac, data2a$public_semipublic_p, data2a$dispute_before, data2a$holds1, data2a$log_MPdist, data2a$prim_other_total, data2a$incumbent)
genout.2a<-GenMatch(Tr=data2a$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=500, max.generations=30, wait.generations=10)

###############################################################
### save workspace
save(data2, data2a, genout.2, genout.2a, genout.2.ATE, genout.2a.ATE, file="matchingoutput_t56SIc.Rdata")




